// composables/useShare.js
import { onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";

export default function useShare(options = {}) {
    // 默认分享配置
    const defaultShareOptions = {
        title: "观界",
        path: "/pages/index/index",
        imageUrl: "/static/images/invite-bg.jpg",
        desc: "",
        content: "",
        query: "", //path后面的query参数：key=value&key2=value2
    };

    // 合并配置
    const shareOptions = Object.assign(defaultShareOptions, options);
    // 获取当前页面路径
    const getCurrentPagePath = () => {
        const pages = getCurrentPages();
        if (pages.length > 0) {
            const currentPage = pages[pages.length - 1];
            let path = "/" + currentPage.route;
            console.log(path, "----path");
            // 如果没有自定义路径，使用当前页面路径
            if (!options.path) {
                // 添加当前页面的查询参数
                if (
                    currentPage.options &&
                    Object.keys(currentPage.options).length > 0
                ) {
                    const query = Object.keys(currentPage.options)
                        .map((key) => `${key}=${currentPage.options[key]}`)
                        .join("&");
                    console.log(query, "----query");
                    shareOptions.query = query;
                }
                shareOptions.path = path;
            }
        }
        return shareOptions.path;
    };
    // 好友分享配置
    onShareAppMessage((res) => {
        console.log("触发分享到好友:", res);
        // 获取当前页面路径
        const currentPath = getCurrentPagePath();
        console.log("currentPath:", currentPath,'-----shareOptions.query-----');
        return {
            title: shareOptions.title,
            path: `${currentPath}${
                shareOptions.query ? `?${shareOptions.query}` : ""
            }`,
            imageUrl: shareOptions.imageUrl,
            desc: shareOptions.desc,
            success: () => {
                uni.showToast({
                    title: "分享成功",
                    duration: 2000,
                    icon: "none",
                });
                shareOptions.success?.();
            },
            fail: (err) => {
                console.error("分享失败:", err);
                uni.showToast({ title: "分享失败", icon: "none" });
            },
        };
    });

    // 朋友圈分享 (仅微信小程序)
    onShareTimeline(() => ({
        title: shareOptions.title,
        query: shareOptions.query,
        imageUrl: shareOptions.imageUrl,
    }));

    // 返回可复用的方法
    return {
		onShareAppMessage,
		onShareTimeline,
        updateShareTitle(newTitle) {
            shareOptions.title = newTitle;
        },
        triggerCustomShare() {
            // uni.share({
            //   type: shareOptions.type || 0,
            //   provider: 'weixin',
            //   title: shareOptions.title,
            //   summary: shareOptions.desc,
            //   href: `https://yourdomain.com${shareOptions.path}`,
            //   imageUrl: shareOptions.imageUrl
            // })
        },
    };
}
